<template>
    <div>
      <el-dialog
        :title="dialogtitle"
        width="800px"
        class="icon-dialog"
        :visible.sync="show"
        :before-close="closeForm"
        append-to-body
        @open="open" >
        <el-form
          ref="form"
          :size="size"
          :model="form"
          :rules="rules"
          :label-width="$store.getters.device !== 'mobile'?'16%':'90px'" >

          <el-row>
            <el-col :span="24">
              <el-form-item label="类型" prop="type">
                <el-radio-group v-model="form.type">
                  <el-radio :label="1" >积分</el-radio>
                  <el-radio :label="2" >余额</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="额度" prop="quota">
                <el-input v-model="form.quota" auto-complete="off" clearable placeholder="请输入额度" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="数量" prop="number">
                <el-input v-model="form.number" auto-complete="off" clearable placeholder="请输入数量" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="截止时间">
                <el-date-picker
                  v-model="form.term_time"
                  type="datetime"
                  placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button :loading="loading" type="primary" @click="submit">
            <span v-if="!loading">保 存</span>
            <span v-else>提 交 中...</span>
          </el-button>
          <el-button @click="closeForm">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  <script>
  import ImagesUpload from '@/components/common/ImagesUpload.vue'
  export default {
    name: 'broadcastupdate',
    components: {
      ImagesUpload
    },
    props: {
      show: {
        type: Boolean,
        default: false
      },
      opentype: {
        type: String,
        default: 'add'
      },
      size: {
        type: String,
        default: 'small'
      },
      info: {
        type: Object
      }
    },
    data() {
      return {
        dialogtitle: '',
        cidarray: [],
        form: {
          type: 1,
          title: '',
          status: 1
        },
        loading: false,
        rules: {
          username: [{
            required: true,
            message: '用户名称不能为空',
            trigger: 'blur'
          }]
        }
      }
    },
    watch: {
      show(val) {
      }
    },
    methods: {
      open() {
        if (this.opentype == 'update') {
          this.dialogtitle = '修改'
          this.form = this.info
        } else {
          this.dialogtitle = '添加'
          this.form = {
            status: 1,
            type: 1
          }
          // console.log(this.form);
        }
      },
      submit() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            this.loading = true
            this.$api.post('/carmine/create', this.form).then(res => {
              this.$message({
                message: res.msg,
                type: 'success'
              })
              this.$emit('refesh_list')
              this.closeForm()
            }).catch(() => {
              this.loading = false
            })
          }
        })
      },
      closeForm() {
        this.$emit('update:show', false)
        this.loading = false
        if (this.$refs['form'] !== undefined) {
          this.$refs['form'].resetFields()
        }
      }
    }
  }
  </script>
  